<?php

/*******************************************************************************
 * JS输出
 *******************************************************************************/

// 利用JS进行跳转
function jump($url, $ttl = 0){
	echo <<<JS
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>正在跳转</title>
</head>
<body>
<script type="text/javascript" language="JavaScript" charset="utf-8">
<!--
	setTimeout(function(){
		window.location.href = '{$url}';		
	}, {$ttl});
-->
</script>
</body>
</html>
JS
;
	exit;
}

/********************************************************************************
 * 表单项
 ********************************************************************************/

// 生成下拉列表项
function make_options($options = array(), $option_empty = '', $selected = ''){
	$option_arr = array();
	if(!empty($option_empty)){
		if($option_empty === true)$option_empty = '';
		$option_arr[] = '<option value="">'.$option_empty.'</option>';
	}
	if(is_array($options)){
		foreach($options as $key => $text){
			if(isset($selected) && $selected !== '' && $key == $selected){
				$sel = ' selected';
			}else{
				$sel = '';
			}
			$option_arr[] = "<option value=\"$key\"$sel>$text</option>";
		}
	}
	return implode("", $option_arr);
}

// 生成复选框组
function make_checkbox_group($name, $options = array(), $checked = array(), $selall = false){
	$box_arr = array('<ul class="chlist">');
	if(is_array($options)){
		foreach($options as $key => $text){
			if(isset($checked) && !empty($checked) && in_array($key, $checked)){
				$sel = 'checked ';
			}else{
				$sel = '';
			}
			$box_arr[] = "<li><label><input type=\"checkbox\" name=\"{$name}[]\" value=\"$key\" $sel class=\"chbox\" />$text</label></li>";
		}
	}
	if($selall == true){
		$box_arr[] = '<li><label><input type="checkbox" name="selall" class="chbox" /><b>::全选</b></label></li>';
	}
	$box_arr[] = '</ul>';
	return implode('', $box_arr);
}

// 生成单选框组
function make_radio_group($name, $options = array(), $checked = '', $clear = false){
	$box_arr = array('<ul class="chlist">');
	if(is_array($options)){
		foreach($options as $key => $text){
			if(isset($checked) && $checked !== '' && $key == $checked){
				$sel = 'checked ';
			}else{
				$sel = '';
			}
			$box_arr[] = "<li><label><input type=\"radio\" name=\"{$name}\" value=\"$key\" $sel/>$text</label></li>";
		}
	}
	if($clear == true){
		$box_arr[] = "<li><label><input type=\"radio\" name=\"{$name}\" value=\"\" $sel/>清除</label></li>";
	}
	$box_arr[] = '</ul>';
	return implode('', $box_arr);
}

/*******************************************************************************
 * 高亮关键词
 *******************************************************************************/
function highlight_word($str, $keywords, $classname = 'highlight'){
	if(empty($keywords))return $str;
	if(is_array($keywords)){
		foreach($keywords as $keyword) {
			$str = preg_replace("/({$keyword})/i", "<span class=\"{$classname}\">$1</span>", $str);
		}
	}else{
		$str = preg_replace("/({$keywords})/i", "<span class=\"{$classname}\">$1</span>", $str);
	}
	return $str;
}

function highlight_arr(&$arr, $keywords, $pre = '', $classname = 'highlight'){
	foreach($keywords as $field => $keyword){
		if(!array_key_exists($field, $arr)){
			unset($keywords[$field]);
		}
	}
	if(empty($keywords))return false;
	if($pre === true)$pre = '_hl_';
	foreach($keywords as $field => $keyword){
		if(empty($keyword)){
			$arr["{$pre}{$field}"] = $arr[$field];
		}else{
			$arr["{$pre}{$field}"] = preg_replace("/({$keyword})/i", "<span class=\"{$classname}\">$1</span>", $arr[$field]);
		}
	}
	return true;
}

function highlight_list(&$list, $keywords, $pre = '', $classname = 'highlight'){
	$arr = $list[0];
	foreach($keywords as $field => $keyword){
		if(!array_key_exists($field, $arr) || empty($keyword)){
			unset($keywords[$field]);
		}
	}
	if(empty($keywords))return false;
	if($pre === true)$pre = '_hl_';
	foreach ($keywords as $field){
		if(empty($keyword)){
			foreach($list as $key => $row){
				$row["{$pre}{$field}"] = $row[$field];
			}
		}else{
			foreach($list as $key => $row){
				$row["{$pre}{$field}"] = preg_replace("/({$keyword})/i", "<span class=\"{$classname}\">$1</span>", $row[$field]);
			}
		}
		$list[$key] = $row;
	}
	return true;
}

/********************************************************************************
 * 重新设置模板默认路径
 ********************************************************************************/

function set_template_path($tpl, $dir) {
	$dir = htmlspecialchars($dir);
	$tpl->template_dir = str_replace('//', '/', $dir.'/');
}

/**
 * 根据当前页码和记录总数返回页码列表值
 */
function pagelist($total, $size, $page, $offset = 5){
	exit('此函数已废弃');
	
	$pages = array();
	$pages['page']		= intval($page);
	$pages['size']		= max(intval($size), 1);
	$pages['total']		= $total;
	$pages['pagecount'] = $pagecount = ceil($total / $size);
	$pages['first']		= 0;
	$pages['prev']		= 0;
	$pages['next']		= 0;
	$pages['last']		= 0;
	
	if($page > 1)$pages['prev'] = $page - 1;
	if($page < $pagecount)$pages['next'] = $page + 1;

	$dset = $offset * 2;
	$begin = $page - $offset;
	if($begin < 1)$begin = 1;
	
	$end = $begin + $dset;
	if($end > $pagecount)$end = $pagecount;
	if($end - $begin < $dset){
		$begin = $end - $dset;
		if($begin < 1)$begin = 1;
	}
	
	if($page - 1 > $offset)$pages['first'] = 1;
	if($pagecount - $page > $offset)$pages['last'] = $pagecount;
	for($i = $begin; $i <= $end; $i ++){
		$pages['pagelist'][] = $i;
	}
	
	return $pages;
}

/*******************************************************************************
 * 后台规格数组转换成字符串
 *******************************************************************************/
function specs_join($specs, $br = '，'){
	/*$specs = array (
		1 => array ( 'name' => '颜色', 'value' => '绿', ),
		2 => array ( 'name' => '尺码', 'value' => 'M', ),
	);*/
	// $br = "\r\n";	// 换行
	$lines = array();
	foreach($specs as $spec){
		$lines[] = implode('：', $spec);
	}
	$lines = implode($br, $lines);
	return $lines;
}
?>
